#include "autoinit.hpp"
#include <iostream>
#include <boost/thread.hpp>
#include <inputmanager.hpp>

#include <elog.h>
#include <stdio.h>
#include <string>
#include <stdio.h>
#include <stdarg.h>

#include <boost/asio.hpp>

#include <inputdev_func.hpp>

#include "efsm.hpp"
#include "efsm_event.hpp"

void DebugLoggerInit(void);


EFSM_DECLEAR(normal_mode_efsmsets);
EFSM_DECLEAR(admin_mode_efsmsets);

EFSM_PTR_CREATE(main_set); 


static int manager_initialization(void)
{
  inputmanager* p_inputmanager = inputmanager::get_inputmanager();

  p_inputmanager->attach_event_listener(normal_state_manmager::get_normal_state_manmager());
  p_inputmanager->attach_event_listener(admin_state_manmager::get_admin_state_manmager());

  return 0;
}


int main()
{
  DebugLoggerInit();
  sys_devices_init();
  
  /* test log output for all level */
  log_a("** Hello EasyLogger!** \n");
  log_e("** Hello EasyLogger!** \n");
  log_w("** Hello EasyLogger!** \n");
  log_i("** Hello EasyLogger!** \n");
  log_d("** Hello EasyLogger!** \n");
  log_v("** Hello EasyLogger!** \n");

  //string obj1 = combine_devtypes(2, key_inputdev_type, fingermodule_inputdev_type);
  //inmanager_enroll(const_cast<char*>(obj1.c_str()), NULL);


  manager_initialization();


  //set_timer2callback_inputdevfunc(4);

  // cancel_the_timer_inputdevfunc();
  boost::this_thread::sleep(boost::posix_time::seconds(1));


 
  EFSM_PTR_BIND(main_set, admin_mode_efsmsets); 
  log_d("****************** begin *********************\n");



#if 1
  /**********
   * 注册成功，返回值status为1
   * 注册失败，返回值举例：
   * execption_id = 1 表示用户操作非法 cancel_reason = 9 表示详情是用户输入的密码过长
   * ***********/
  input_dev_param enroll_pw_usr_para;
  event_invoke_block1(event_enroll_password_usr,  (void*)(&enroll_pw_usr_para));

  if(input_no_ececption != enroll_pw_usr_para.execption_id){
    /* input_kb_keyspressed_tooshort:2  ***/
    log_w(".execption_id = %d , cancel_reason = %d\n",    \
          enroll_pw_usr_para.execption_id, enroll_pw_usr_para.cancel_reason);
  }
  
  log_w(".status = %d \n", enroll_pw_usr_para.status);
  
#endif 

  log_d("==========enroll over============\n\n");

  log_d("==========begin auth============\n");

#if 1
  set_timer2callback_inputdevfunc(4);

  input_dev_param auth_pw_usr_para;
  event_invoke_block1(event_auth_permission,  (void*)(&auth_pw_usr_para));

  if(input_no_ececption != auth_pw_usr_para.execption_id){

    log_w(".execption_id = %d , cancel_reason = %d\n",    \
         auth_pw_usr_para.execption_id, auth_pw_usr_para.cancel_reason);
  }
  
  log_w(".status = %d \n", auth_pw_usr_para.status);
#endif


  while(1){
    
    log_i("-main  loop-\n");
    boost::this_thread::sleep(boost::posix_time::seconds(1));

  }

  return 0;
}



void DebugLoggerInit(void)
{
    /*
    - `elog_set_output_enabled(false);
    - `elog_set_filter_lvl(ELOG_LVL_WARN);
    - `elog_set_filter_tag("main");
    - `elog_set_filter_kw("Hello");
    */
    /* close printf buffer */
    setbuf(stdout, NULL);
    /* initialize EasyLogger */
    elog_init();
    /* set EasyLogger log format */
    elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_ERROR,ELOG_FMT_LVL | ELOG_FMT_TAG ); //ELOG_FMT_TIME
    elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG );
    elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG );
    elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_LVL); // ELOG_FMT_FUNC
    elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC & (~ELOG_FMT_TIME));
    #ifdef ELOG_COLOR_ENABLE
        elog_set_text_color_enabled(true);
    #endif
    /* start EasyLogger */
    elog_start();
}
